<template>
  <div class="login_container">
    <!--login 盒子-->
    <div class="login_box">
      <!--头像-->
      <div class="login_avatar">
        <img src="https://img-blog.csdnimg.cn/2020102820095920.jpg">
      </div>
      <!--登录表单区域-->
      <el-form ref="loginFormRef" :model="form" :rules="rules" label-width="0px" class="login_form">
        <!--username-->
        <el-form-item prop="username">
          <el-input ref="usernameRef" v-model="form.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <!--password-->
        <el-form-item prop="password">
          <el-input type="password" v-model="form.password" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <!--button-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

  import {getLogin} from '../../../network/login.js'

  export default {
    data() {
      return {
        form: {
          username: 'admin',
          password: '123456'
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 4, max: 12, message: '长度在 4 到 12 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      reset() {
        this.$refs.loginFormRef.resetFields()
        this.$refs.usernameRef.focus();
      },
      login() {
        this.$refs.loginFormRef.validate(async valid => {
          if (!valid) return;
          /*const {data : result} = await this.$http.post('login',this.form)*/
          this.$http.post('login',this.form).then(result => {
            if (result.data.meta.status == 200) {
              this.$message.success("登录成功!");
              /**
               * 1．将登录成功之后的 token，保存到客户端的sessionStorage中
               * 2. 项目中除了登录之外的其他API接口，必须在登录之后才能访问
               3. token只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage
               4．通过编程式导航跳转到后台主页，路由地址是/home
               */
              window.sessionStorage.setItem("token",result.data.data.token);
              this.$router.push("/home");
            } else this.$message.error("登录失败!");

          })

          /** 封装方式发送请求
          getLogin(this.form).then(result => {
            if (result.data.meta.status == 200)console.log("登录成功!"); else console.log("登录失败");
          })*/



        } )
      }
    }
  }
</script>

<style lang="less" scoped>
  .login_container {
    height: 100%;
    width: 100%;
    background-color: #2b4b6b;
  }

  .login_box {
    width: 450px;
    height: 350px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

  .login_avatar{
    height: 130px;
    width: 130px;
    border-radius: 50%;
    border: 1px solid grey;
    box-shadow: 0 0 10px #ddd;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
    img{
      height: 100%;
      width: 100%;
      border-radius: 50%;
    }
  }

  .btns {
    display: flex;
  }

  .login_form {
    position: absolute;
    bottom: 50px;
    width: 100%;
    padding: 0px 20px;
    box-sizing: border-box;

  }

</style>
